
//
//  JKPersonInfoCell.m
//  JKRecover
//
//  Created by Veer on 2018/6/3.
//  Copyright © 2018年 Veer. All rights reserved.
//

#import "JKPersonInfoCell.h"

#import "JKPersonInfoConfigModel.h"


@interface JKPersonInfoCell() <UITextFieldDelegate>

@property (nonatomic, weak) UILabel *titleLbl;
//@property (nonatomic, weak) UILabel *detailLbl;
@property (nonatomic, strong) UITextField *inputTf;
@property (nonatomic, assign) UIView *bottomLineView;
@property (nonatomic, weak) UIImageView *accessoryImageView;


@end

@implementation JKPersonInfoCell

+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"JKPersonInfoCell";
    
    JKPersonInfoCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        
        cell = [[JKPersonInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.accessoryType = UITableViewCellAccessoryNone;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // lieview
        [self setupSubView];
        
    }
    return self;
}


- (void)setupSubView
{
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.textColor = RGB(53, 53, 53);
    titleLbl.font = VRFont(16);
    titleLbl.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:titleLbl];
    self.titleLbl = titleLbl;
    
    UITextField *inputTf = [[UITextField alloc] init];
    inputTf.delegate = self;
    inputTf.textColor = RGB(51, 51, 51);
    inputTf.font = VRFont(16);
    [self.contentView addSubview:inputTf];
    self.inputTf = inputTf;
    [inputTf addTarget:self action:@selector(textFieldTextChanged:) forControlEvents:UIControlEventEditingChanged];
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = RGB(228, 228, 228);
    [self.contentView addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;
    
    UIImageView *accessoryImageView = [[UIImageView alloc] init];
    accessoryImageView.contentMode = UIViewContentModeScaleAspectFill;
    accessoryImageView.image = [UIImage imageNamed:@"icon_jump_arrow"];
    [self.contentView addSubview:accessoryImageView];
    self.accessoryImageView = accessoryImageView;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.contentView);
    }];
    
    [self.inputTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(120);
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(40);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(15);
        make.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.accessoryImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView);
        make.width.mas_equalTo(9);
        make.height.mas_equalTo(13);
    }];
}


#pragma mark - delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    if (self.textFieldClickBlock) {
        self.textFieldClickBlock(textField);
    }
    return self.model.isCanEdit;
}

- (void)textFieldTextChanged:(UITextField *)textField
{
    self.model.value = textField.text;
}

#pragma mark - setter && getter
- (void)setModel:(JKPersonInfoConfigModel *)model
{
    _model = model;
    
    self.titleLbl.text = model.name;
    self.inputTf.placeholder = model.des;
    self.inputTf.text = model.value;
    if (model.isPassword) {
        self.inputTf.secureTextEntry = YES;
    }
    
    self.accessoryImageView.hidden = model.isCanEdit;
    if (!model.isCanEdit && model.isHideAccessory) {
        self.accessoryImageView.hidden = YES;
    }
    if (model.name.length == 0) {
        
        [self.bottomLineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(115);
        }];
    }else{
        [self.bottomLineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
        }];
    }

}

@end
